package com.weiaibenpao.demo.chislim;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZJL on 2017/8/26.
 */

public class RunningDataView extends RelativeLayout {
    @BindView(R.id.iv_expand)
    ImageView expand;
    @BindView(R.id.iv_collapse)
    ImageView collapse;
    @BindView(R.id.root)
    RelativeLayout dataLayout;

    public RunningDataView(Context context) {
        super(context);
        init(context);
    }

    public RunningDataView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RunningDataView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RunningDataView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        //该布局非本人所写，原布局嵌套太多，简单修改了一下，时间紧迫，后续再优化（用一层RelativeLayout或最多两层即可完成）
        View view = LayoutInflater.from(context).inflate(R.layout.layout_running_data, this);
        ButterKnife.bind(this, view);
    }

    /**
     * 进入跑步界面发现动画偶尔有些卡顿---已证明是由于Map_Service引起的，因为Service中的操作默认都是在UI线程中的！！！
     * 注意：View动画是对View影像做动画，动画后View的真实位置没有变动，可见性都是没变的（除非你动画后手动改变了），所以动画过程中onMeasure和onLayout并不会执行
     * 通过 getGlobalVisibleRect判断View可见是无效的
     */

    public void setView(Bundle data) {




    }

    @OnClick({R.id.iv_expand, R.id.iv_collapse})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_expand:
                dataLayout.startAnimation(showAnimation(700, true));
                dataLayout.setVisibility(VISIBLE);
//                expand.startAnimation(hideAnimation(500, false));
                break;
            case R.id.iv_collapse:
                dataLayout.startAnimation(hideAnimation(700, true));
                dataLayout.setVisibility(INVISIBLE);
//                expand.startAnimation(showAnimation(500, false));
                break;
        }
    }


    private Animation showAnimation(int duration,boolean setListener) {
        TranslateAnimation showAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        showAnimation.setDuration(duration);
        showAnimation.setFillAfter(true);
       if(setListener){
           showAnimation.setAnimationListener(new Animation.AnimationListener() {
               @Override
               public void onAnimationStart(Animation animation) {

               }

               @Override
               public void onAnimationEnd(Animation animation) {
                   expand.postDelayed(new Runnable() {
                       @Override
                       public void run() {
                           expand.startAnimation(hideAnimation(700,false));
                           expand.setVisibility(INVISIBLE);
                       }
                   },200);


               }

               @Override
               public void onAnimationRepeat(Animation animation) {

               }
           });
       }
        return showAnimation;
    }

    private Animation hideAnimation(int duration,boolean setListener) {
        TranslateAnimation hideAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        hideAnimation.setDuration(duration);
        hideAnimation.setFillAfter(true);
        if(setListener){
            hideAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    expand.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            expand.startAnimation(showAnimation(700,false));
                            expand.setVisibility(VISIBLE);
                        }
                    },200);

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
        return hideAnimation;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        clearAnimation();
//        Log.e("onDetachedFromWindow","clear animation");
    }


    //    @Override
//    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        super.onLayout(changed, l, t, r, b);
//        Log.e("RunningDataViw","onLayout");
//    }
//
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.e("RunningDataViw","onMeasure");
//    }
}
